<?php
namespace app\home\controller;

use app\home\common\Common;
use app\home\common\Page;
use app\home\common\Pagetwo;
use app\infrastructure\uri\Request;
use app\infrastructure\uri\Uri;
use app\home\service\Smstmp;
use think\Db;


/**
 * Created by PhpStorm.
 * User: YD
 * Date: 2017/4/26
 * Time: 16:52
 */
class Transaction extends Common
{
    private $client;
    public  function  __construct(){
        parent::__construct();
        $this->client=new Request();
    }
    /**
     * @return \think\response\View
     * 待交易列表
     */
    public function transactionList(){
        $query=[];
        $query['payed_info']=[
//            '$eq'=>null
            '$or'=>[
              null,0
            ]
        ];
        if(session('role') == 'admin'){
            //线上
            $query['user_phone']=[
                '$notlike'=>"1%"
            ];
        }elseif(session('role') == 'testing'){
            //测试
            $query['user_phone']=[
                '$like'=>'1%'
            ];
        }
        if(isset($_GET['Phone']) && !empty($_GET['Phone'])){
            $query['user_phone']=$_GET['Phone'];
        }
        if(isset($_GET['manyUserPhone']) && !empty($_GET['manyUserPhone'])){
            $manyUserPhone=trim($_GET['manyUserPhone']);
            if(!empty($manyUserPhone)){
                if(strpos($manyUserPhone,',')){
                    $newManyUserPhone=explode(',',$manyUserPhone);
                }else{
                    $manyUserPhone=explode("\r\n",$manyUserPhone);
                    $newManyUserPhone=[];
                    if(!empty($manyUserPhone)){
                        foreach($manyUserPhone as $k=>$v){
                            $newManyUserPhone[]=trim($v);
                        }
                    }
                }
                $creteNewUserPhone=implode(',',$newManyUserPhone);
                $query['user_phone']=$newManyUserPhone;
                $_GET['manyUserPhone']=$creteNewUserPhone;
            }
        }
        if(!empty($_GET['upCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000
            ];
        }
        if(!empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        if(!empty($_GET['upCreateTime']) && !empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000,
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        $query['payed_status']='pending';
        $query['limitation']=10000;
        $countUrl=Uri::HOST.Uri::POSTSELECTUSERLOANTRANSACTION;
        $totalCount =$this->client->getArrayData($countUrl,$query,3);
        if(isset($_GET['extend']) && ($_GET['extend'] == 1 || $_GET['extend'] == 2)){
            $extendData= $totalCount['data'];
            $newData=[];
            foreach($extendData as $k=>$v){
             //查找个人认证
              $usercerUrl=Uri::HOST.Uri::USERCERITYS."?phoneNumber=".$v['user_phone'];
              $userCerData=$this->client->getArrayData($usercerUrl);
              $userCerData=$userCerData['data'][0];
                //查找绑卡信息
                $bankUrl=Uri::HOST.Uri::SELECTUSERBANK.$v["user_id"];
                $bankData=$this->client->getArrayData($bankUrl);
                $bankData=$bankData['data'];

                //查询用户personal信息
                $personalUrl=Uri::HOST.Uri::SELECTPERSONINFO."?user_id=".$v['user_id'];
                $personalData=$this->client->getArrayData($personalUrl);
                $personalData=$personalData['data'][0];

                //查询userloads信息
                $userloanUrl=Uri::HOST.Uri::SELECTUSERLOAN."?id=".$v['user_loan_id'];
                $userLoanData=$this->client->getArrayData($userloanUrl);
                $userLoanData=$userLoanData['data'][0];
                $repaymentDate=$v['update_time'];
                $repaymentDate=strtotime($repaymentDate) + 3600*24*($userLoanData['loanRepayPeriod']-1);
                $repaymentDate=date("d-m-Y",$repaymentDate);
                $disbursementDate=strtotime($v['create_time']);
                $disbursementDate=date("d-m-Y",$disbursementDate);

                $birthDay=$personalData['birthDay'];
                if(!empty($personalData['birthDay'])){
                    $birthDay=explode('-',$birthDay);
                    $birthDay=$birthDay[2]."-".$birthDay[1]."-".$birthDay[0];
                }else{
                    $birthDay= $birthDay;
                }

                if($personalData['genderinfo'] == 'male'){
                    $gender=0;
                }elseif($personalData['genderinfo'] == 'female'){
                    $gender=1;
                }
                $newData[]=[
                    'transaction ID'=>$v['id'],
                    'Loan ID'=>substr($v['user_loan_id'],0,30),
                    'Customer ID'=>substr($v['user_id'],0,30),
                    'First Name'=>$userCerData['firstname'],
                    'Middle Name'=>$userCerData['middlename'],
                    'Last Name'=>$userCerData['lastname'],
                    'DOB'=>$birthDay,
                    'Mobile'=>$v['user_phone'],
                    'Email'=>$userCerData['email'],
                    'Gender'=>$gender,
                    'Aadhaar Number'=>$userCerData['aadhaar_num'],
                    'PAN Number'=>$personalData['panID'],
                    'Current Address 1'=>$personalData['currentAddress'],
                    'Current Address 2'=>$personalData['currentAddress'],
                    'Current Address 3'=>$personalData['currentAddress'],
                    'Current State'=>$personalData['currentState'],
                    'Current City'=>$personalData['currentCity'],
                    'Current Pincode'=>$personalData['currentPinCode'],
                    'Permanent Address 1'=>$personalData['permanentaAddress'],
                    'Permanent Address 2'=>$personalData['permanentaAddress'],
                    'Permanent Address 3'=>$personalData['permanentaAddress'],
                    'Permanent State'=>$personalData['permanentState'],
                    'Permanent City'=>$personalData['permanentCity'],
                    'Permanent Pincode'=>$personalData['permanentaPinCode'],
                    'Bank Name'=>$bankData['card_type'],
                    'Bank Account Number'=>$bankData['ptm_card'],
                    'Bank IFSC Code'=>$bankData['nbfc_num'],
                    'Bank Branch Name'=>'',
                    'Disbursement Date'=>$disbursementDate,
                    'Loan Amount'=>$v['money'],
                    'Tenure'=>$userLoanData['loanRepayPeriod'],
                    'ROI'=>$userLoanData['loanInterestRate'],
                    'Fee'=>$userLoanData['auditFee'],
                    'First repayment Date'=>$repaymentDate,
                ];
            }
            $column_name=[
                [
                    'Transaction ID',
                    'Loan ID',
                    'Customer ID',
                    'First Name',
                    'Middle Name',
                    'Last Name',
                    'DOB',
                    'Mobile',
                    'Email',
                    'Gender',
                    'Aadhaar Number',
                    'PAN Number',
                    'Current Address 1',
                    'Current Address 2',
                    'Current Address 3',
                    'Current State',
                    'Current City',
                    'Current Pincode',
                    'Permanent Address 1',
                    'Permanent Address 2',
                    'Permanent Address 3',
                    'Permanent State',
                    'Permanent City',
                    'Permanent Pincode',
                    'Bank Name',
                    'Bank Account Number',
                    'Bank IFSC Code',
                    'Bank Branch Name',
                    'Disbursement Date',
                    'Loan Amount',
                    'Tenure',
                    'ROI',
                    'Fee',
                    'First repayment Date'
                ]
            ];
            $data = array_merge($column_name,$newData);
            if($_GET['extend'] == 2){
                $this->extendToCsvNoTab($data);
            }else{
                $this->extendToCsv($data);
            }
//            $data = array_merge($column_name,$newData);
//            $this->extendToExcel($data);
        }
        $p=empty(input('p'))?1:input('p');
        $startIndex=($p-1)*15;
        $query['limitation']=15;
        $query['offset']=$startIndex;
        $value = $this->client->getArrayData($countUrl,$query,3);
        if($value['code'] != 0){
            $value['data']=[];
        }
        $pager=new Page(count($totalCount['data']),15,$p);
        $page=$pager->show();
        $this->assign('page',$page);
        foreach($value['data'] as $k=>$v){
            $value['data'][$k]['create_time']=$this->gmtToIndiaTime($v['create_time']);
            $value['data'][$k]['update_time']=$this->gmtToIndiaTime($v['update_time']);
        }
        $this->assign('data',$value['data']);
        $this->assign('totalCount',count($totalCount['data']));
        $this->assign('allData',$totalCount['data']);
        $this->assign('totalData',json_encode($totalCount['data']));
        return view();
    }

    /**
     *  已签证待交易列表
     */
    public function transactionEsignList(){
        $query=[];
        $query['payed_info']=[
            '$or'=>[
                null,0
            ]
        ];
        if(session('role') == 'admin'){
            //线上
            $query['user_phone']=[
                '$notlike'=>"1%"
            ];
        }
        elseif(session('role') == 'testing'){
            //测试
            $query['user_phone']=[
                '$like'=>'1%'
            ];
        }
        if(isset($_GET['Phone']) && !empty($_GET['Phone'])){
            $query['user_phone']=$_GET['Phone'];
        }
        if(isset($_GET['manyUserPhone']) && !empty($_GET['manyUserPhone'])){
            $manyUserPhone=trim($_GET['manyUserPhone']);
            if(!empty($manyUserPhone)){
                if(strpos($manyUserPhone,',')){
                    $newManyUserPhone=explode(',',$manyUserPhone);
                }else{
                    $manyUserPhone=explode("\r\n",$manyUserPhone);
                    $newManyUserPhone=[];
                    if(!empty($manyUserPhone)){
                        foreach($manyUserPhone as $k=>$v){
                            $newManyUserPhone[]=trim($v);
                        }
                    }
                }
                $creteNewUserPhone=implode(',',$newManyUserPhone);
                $query['user_phone']=$newManyUserPhone;
                $_GET['manyUserPhone']=$creteNewUserPhone;
            }
        }
        if(!empty($_GET['upCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000
            ];
        }
        if(!empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        if(!empty($_GET['upCreateTime']) && !empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000,
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        $query['payed_status']='pending';
        $query['esign_status']=1;
        $query['limitation']=10000;
        $countUrl=Uri::HOST.Uri::POSTSELECTUSERLOANTRANSACTION;
        $totalCount =$this->client->getArrayData($countUrl,$query,3);
        if(isset($_GET['extend']) && ($_GET['extend'] == 1 || $_GET['extend'] == 2)){
            $extendData= $totalCount['data'];
            $newData=[];
            foreach($extendData as $k=>$v){
                //查找个人认证
                $usercerUrl=Uri::HOST.Uri::USERCERITYS."?phoneNumber=".$v['user_phone'];
                $userCerData=$this->client->getArrayData($usercerUrl);
                $userCerData=$userCerData['data'][0];
                //查找绑卡信息
                $bankUrl=Uri::HOST.Uri::SELECTUSERBANK.$v["user_id"];
                $bankData=$this->client->getArrayData($bankUrl);
                $bankData=$bankData['data'];

                //查询用户personal信息
                $personalUrl=Uri::HOST.Uri::SELECTPERSONINFO."?user_id=".$v['user_id'];
                $personalData=$this->client->getArrayData($personalUrl);
                $personalData=$personalData['data'][0];

                //查询userloads信息
                $userloanUrl=Uri::HOST.Uri::SELECTUSERLOAN."?id=".$v['user_loan_id'];
                $userLoanData=$this->client->getArrayData($userloanUrl);
                $userLoanData=$userLoanData['data'][0];
                $repaymentDate=$v['update_time'];
                $repaymentDate=strtotime($repaymentDate) + 3600*24*($userLoanData['loanRepayPeriod']-1);
                $repaymentDate=date("d-m-Y",$repaymentDate);
                $disbursementDate=strtotime($v['create_time']);
                $disbursementDate=date("d-m-Y",$disbursementDate);

                $birthDay=$personalData['birthDay'];
                if(!empty($personalData['birthDay'])){
                    $birthDay=explode('-',$birthDay);
                    $birthDay=$birthDay[2]."-".$birthDay[1]."-".$birthDay[0];
                }else{
                    $birthDay= $birthDay;
                }

                if($personalData['genderinfo'] == 'male'){
                    $gender=0;
                }elseif($personalData['genderinfo'] == 'female'){
                    $gender=1;
                }
                $newData[]=[
                    'transaction ID'=>$v['id'],
                    'Loan ID'=>substr($v['user_loan_id'],0,30),
                    'Customer ID'=>substr($v['user_id'],0,30),
                    'First Name'=>$userCerData['firstname'],
                    'Middle Name'=>$userCerData['middlename'],
                    'Last Name'=>$userCerData['lastname'],
                    'DOB'=>$birthDay,
                    'Mobile'=>$v['user_phone'],
                    'Email'=>$userCerData['email'],
                    'Gender'=>$gender,
                    'Aadhaar Number'=>$userCerData['aadhaar_num'],
                    'PAN Number'=>$personalData['panID'],
                    'Current Address 1'=>$personalData['currentAddress'],
                    'Current Address 2'=>$personalData['currentAddress'],
                    'Current Address 3'=>$personalData['currentAddress'],
                    'Current State'=>$personalData['currentState'],
                    'Current City'=>$personalData['currentCity'],
                    'Current Pincode'=>$personalData['currentPinCode'],
                    'Permanent Address 1'=>$personalData['permanentaAddress'],
                    'Permanent Address 2'=>$personalData['permanentaAddress'],
                    'Permanent Address 3'=>$personalData['permanentaAddress'],
                    'Permanent State'=>$personalData['permanentState'],
                    'Permanent City'=>$personalData['permanentCity'],
                    'Permanent Pincode'=>$personalData['permanentaPinCode'],
                    'Bank Name'=>$bankData['card_type'],
                    'Bank Account Number'=>$bankData['ptm_card'],
                    'Bank IFSC Code'=>$bankData['nbfc_num'],
                    'Bank Branch Name'=>'',
                    'Disbursement Date'=>$disbursementDate,
                    'Loan Amount'=>$v['money'],
                    'Tenure'=>$userLoanData['loanRepayPeriod'],
                    'ROI'=>$userLoanData['loanInterestRate'],
                    'Fee'=>$userLoanData['auditFee'],
                    'First repayment Date'=>$repaymentDate,
                ];
            }
            $column_name=[
                [
                    'Transaction ID',
                    'Loan ID',
                    'Customer ID',
                    'First Name',
                    'Middle Name',
                    'Last Name',
                    'DOB',
                    'Mobile',
                    'Email',
                    'Gender',
                    'Aadhaar Number',
                    'PAN Number',
                    'Current Address 1',
                    'Current Address 2',
                    'Current Address 3',
                    'Current State',
                    'Current City',
                    'Current Pincode',
                    'Permanent Address 1',
                    'Permanent Address 2',
                    'Permanent Address 3',
                    'Permanent State',
                    'Permanent City',
                    'Permanent Pincode',
                    'Bank Name',
                    'Bank Account Number',
                    'Bank IFSC Code',
                    'Bank Branch Name',
                    'Disbursement Date',
                    'Loan Amount',
                    'Tenure',
                    'ROI',
                    'Fee',
                    'First repayment Date'
                ]
            ];
            $data = array_merge($column_name,$newData);
            if($_GET['extend'] == 2){
                $this->extendToCsvNoTab($data);
            }else{
                $this->extendToCsv($data);
            }
        }
        $p=empty(input('p'))?1:input('p');
        $startIndex=($p-1)*15;
        $query['limitation']=15;
        $query['offset']=$startIndex;
        $value = $this->client->getArrayData($countUrl,$query,3);
        if($value['code'] != 0){
            $value['data']=[];
        }
        $pager=new Page(count($totalCount['data']),15,$p);
        $page=$pager->show();
        $this->assign('page',$page);
        foreach($value['data'] as $k=>$v){
            $value['data'][$k]['create_time']=$this->gmtToIndiaTime($v['create_time']);
            $value['data'][$k]['update_time']=$this->gmtToIndiaTime($v['update_time']);
        }
        $this->assign('data',$value['data']);
        $this->assign('totalCount',count($totalCount['data']));
        $this->assign('allData',$totalCount['data']);
        $this->assign('totalData',json_encode($totalCount['data']));
        return view();
    }

    /**
     *  待签证待交易列表
     */
    public function transactionNoEsignList(){
        $query=[];
        $query['payed_info']=[
            '$or'=>[
                null,0
            ]
        ];
        if(session('role') == 'admin'){
            //线上
            $query['user_phone']=[
                '$notlike'=>"1%"
            ];
        }
        elseif(session('role') == 'testing'){
            //测试
            $query['user_phone']=[
                '$like'=>'1%'
            ];
        }
        if(isset($_GET['Phone']) && !empty($_GET['Phone'])){
            $query['user_phone']=$_GET['Phone'];
        }
        if(isset($_GET['manyUserPhone']) && !empty($_GET['manyUserPhone'])){
            $manyUserPhone=trim($_GET['manyUserPhone']);
            if(!empty($manyUserPhone)){
                if(strpos($manyUserPhone,',')){
                    $newManyUserPhone=explode(',',$manyUserPhone);
                }else{
                    $manyUserPhone=explode("\r\n",$manyUserPhone);
                    $newManyUserPhone=[];
                    if(!empty($manyUserPhone)){
                        foreach($manyUserPhone as $k=>$v){
                            $newManyUserPhone[]=trim($v);
                        }
                    }
                }
                $creteNewUserPhone=implode(',',$newManyUserPhone);
                $query['user_phone']=$newManyUserPhone;
                $_GET['manyUserPhone']=$creteNewUserPhone;
            }
        }
        if(!empty($_GET['upCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000
            ];
        }
        if(!empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        if(!empty($_GET['upCreateTime']) && !empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000,
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        $query['payed_status']='pending';
        $query['esign_status']=0;
        $query['limitation']=10000;
        $countUrl=Uri::HOST.Uri::POSTSELECTUSERLOANTRANSACTION;
        $totalCount =$this->client->getArrayData($countUrl,$query,3);
        if(isset($_GET['extend']) && ($_GET['extend'] == 1 || $_GET['extend'] == 2)){
            $extendData= $totalCount['data'];
            $newData=[];
            foreach($extendData as $k=>$v){
                //查找个人认证
                $usercerUrl=Uri::HOST.Uri::USERCERITYS."?phoneNumber=".$v['user_phone'];
                $userCerData=$this->client->getArrayData($usercerUrl);
                $userCerData=$userCerData['data'][0];
                //查找绑卡信息
                $bankUrl=Uri::HOST.Uri::SELECTUSERBANK.$v["user_id"];
                $bankData=$this->client->getArrayData($bankUrl);
                $bankData=$bankData['data'];

                //查询用户personal信息
                $personalUrl=Uri::HOST.Uri::SELECTPERSONINFO."?user_id=".$v['user_id'];
                $personalData=$this->client->getArrayData($personalUrl);
                $personalData=$personalData['data'][0];

                //查询userloads信息
                $userloanUrl=Uri::HOST.Uri::SELECTUSERLOAN."?id=".$v['user_loan_id'];
                $userLoanData=$this->client->getArrayData($userloanUrl);
                $userLoanData=$userLoanData['data'][0];
                $repaymentDate=$v['update_time'];
                $repaymentDate=strtotime($repaymentDate) + 3600*24*($userLoanData['loanRepayPeriod']-1);
                $repaymentDate=date("d-m-Y",$repaymentDate);
                $disbursementDate=strtotime($v['create_time']);
                $disbursementDate=date("d-m-Y",$disbursementDate);

                $birthDay=$personalData['birthDay'];
                if(!empty($personalData['birthDay'])){
                    $birthDay=explode('-',$birthDay);
                    $birthDay=$birthDay[2]."-".$birthDay[1]."-".$birthDay[0];
                }else{
                    $birthDay= $birthDay;
                }

                if($personalData['genderinfo'] == 'male'){
                    $gender=0;
                }elseif($personalData['genderinfo'] == 'female'){
                    $gender=1;
                }
                $newData[]=[
                    'transaction ID'=>$v['id'],
                    'Loan ID'=>substr($v['user_loan_id'],0,30),
                    'Customer ID'=>substr($v['user_id'],0,30),
                    'First Name'=>$userCerData['firstname'],
                    'Middle Name'=>$userCerData['middlename'],
                    'Last Name'=>$userCerData['lastname'],
                    'DOB'=>$birthDay,
                    'Mobile'=>$v['user_phone'],
                    'Email'=>$userCerData['email'],
                    'Gender'=>$gender,
                    'Aadhaar Number'=>$userCerData['aadhaar_num'],
                    'PAN Number'=>$personalData['panID'],
                    'Current Address 1'=>$personalData['currentAddress'],
                    'Current Address 2'=>$personalData['currentAddress'],
                    'Current Address 3'=>$personalData['currentAddress'],
                    'Current State'=>$personalData['currentState'],
                    'Current City'=>$personalData['currentCity'],
                    'Current Pincode'=>$personalData['currentPinCode'],
                    'Permanent Address 1'=>$personalData['permanentaAddress'],
                    'Permanent Address 2'=>$personalData['permanentaAddress'],
                    'Permanent Address 3'=>$personalData['permanentaAddress'],
                    'Permanent State'=>$personalData['permanentState'],
                    'Permanent City'=>$personalData['permanentCity'],
                    'Permanent Pincode'=>$personalData['permanentaPinCode'],
                    'Bank Name'=>$bankData['card_type'],
                    'Bank Account Number'=>$bankData['ptm_card'],
                    'Bank IFSC Code'=>$bankData['nbfc_num'],
                    'Bank Branch Name'=>'',
                    'Disbursement Date'=>$disbursementDate,
                    'Loan Amount'=>$v['money'],
                    'Tenure'=>$userLoanData['loanRepayPeriod'],
                    'ROI'=>$userLoanData['loanInterestRate'],
                    'Fee'=>$userLoanData['auditFee'],
                    'First repayment Date'=>$repaymentDate,
                ];
            }
            $column_name=[
                [
                    'Transaction ID',
                    'Loan ID',
                    'Customer ID',
                    'First Name',
                    'Middle Name',
                    'Last Name',
                    'DOB',
                    'Mobile',
                    'Email',
                    'Gender',
                    'Aadhaar Number',
                    'PAN Number',
                    'Current Address 1',
                    'Current Address 2',
                    'Current Address 3',
                    'Current State',
                    'Current City',
                    'Current Pincode',
                    'Permanent Address 1',
                    'Permanent Address 2',
                    'Permanent Address 3',
                    'Permanent State',
                    'Permanent City',
                    'Permanent Pincode',
                    'Bank Name',
                    'Bank Account Number',
                    'Bank IFSC Code',
                    'Bank Branch Name',
                    'Disbursement Date',
                    'Loan Amount',
                    'Tenure',
                    'ROI',
                    'Fee',
                    'First repayment Date'
                ]
            ];
            $data = array_merge($column_name,$newData);
            if($_GET['extend'] == 2){
                $this->extendToCsvNoTab($data);
            }else{
                $this->extendToCsv($data);
            }
        }
        $p=empty(input('p'))?1:input('p');
        $startIndex=($p-1)*15;
        $query['limitation']=15;
        $query['offset']=$startIndex;
        $value = $this->client->getArrayData($countUrl,$query,3);
        if($value['code'] != 0){
            $value['data']=[];
        }
        $pager=new Page(count($totalCount['data']),15,$p);
        $page=$pager->show();
        $this->assign('page',$page);
        foreach($value['data'] as $k=>$v){
            $value['data'][$k]['create_time']=$this->gmtToIndiaTime($v['create_time']);
            $value['data'][$k]['update_time']=$this->gmtToIndiaTime($v['update_time']);
        }
        $this->assign('data',$value['data']);
        $this->assign('totalCount',count($totalCount['data']));
        $this->assign('allData',$totalCount['data']);
        $this->assign('totalData',json_encode($totalCount['data']));
        return view();
    }

    /**
     * @return \think\response\View
     * 交易成功列表
     */
    public function transactionYesList(){
        $query=[];
        if(session('role') == 'admin'){
            //线上
            $query['user_phone']=[
                '$or'=>[
                    ['$like'=>'9%'],
                    ['$like'=>'8%'],
                    ['$like'=>'7%'],
                    ['$like'=>'6%'],
                    ['$like'=>'5%'],
                    ['$like'=>'4%'],
                    ['$like'=>'3%'],
                    ['$like'=>'2%']
                ]
            ];
        }elseif(session('role') == 'testing'){
            //测试
            $query['user_phone']=[
                '$like'=>'1%'
            ];
        }
        if(!empty($_GET['userId'])){
            $userId=$_GET['userId'];
            $userId=explode(' ',$userId);
            $query['user_id']=[
                '$in'=>$userId
            ];
        }
        if(isset($_GET['userPhone']) && !empty($_GET['userPhone'])){
            $query['user_phone']=$_GET['userPhone'];
        }
        if(!empty($_GET['upCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000
            ];
        }
        if(!empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        if(!empty($_GET['upCreateTime']) && !empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000,
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        $query['payed_status']='resolve';
        $query['limitation']=10000;
        $countUrl=Uri::HOST.Uri::POSTSELECTUSERLOANTRANSACTION;
        $totalCount =$this->client->getArrayData($countUrl,$query,3);
        if(isset($_GET['extend']) && $_GET['extend'] == 1){
            $extendData= $totalCount['data'];
            $newData=[];
            foreach($extendData as $k=>$v){
                //查找个人认证
                $usercerUrl=Uri::HOST.Uri::USERCERITYS."?phoneNumber=".$v['user_phone'];
                $userCerData=$this->client->getArrayData($usercerUrl);
                $userCerData=$userCerData['data'][0];
                //查找绑卡信息
                $bankUrl=Uri::HOST.Uri::SELECTUSERBANK.$v["user_id"];
                $bankData=$this->client->getArrayData($bankUrl);
                $bankData=$bankData['data'];

                //查询用户personal信息
                $personalUrl=Uri::HOST.Uri::SELECTPERSONINFO."?user_id=".$v['user_id'];
                $personalData=$this->client->getArrayData($personalUrl);
                $personalData=$personalData['data'][0];


                //查询userloads信息
                $userloanUrl=Uri::HOST.Uri::SELECTUSERLOAN."?id=".$v['user_loan_id'];
                $userLoanData=$this->client->getArrayData($userloanUrl);
                $userLoanData=$userLoanData['data'][0];
                $repaymentDate=$v['update_time'];
                $repaymentDate=strtotime($repaymentDate) + 3600*24*($userLoanData['loanRepayPeriod']-1);
                $repaymentDate=date("d-m-Y",$repaymentDate);
                $disbursementDate=strtotime($v['create_time']);
                $disbursementDate=date("d-m-Y",$disbursementDate);

                $birthDay=$personalData['birthDay'];
                if(!empty($personalData['birthDay'])){
                    $birthDay=explode('-',$birthDay);
                    $birthDay=$birthDay[2]."-".$birthDay[1]."-".$birthDay[0];
                }else{
                    $birthDay= $birthDay;
                }

                if($personalData['genderinfo'] == 'male'){
                    $gender=0;
                }elseif($personalData['genderinfo'] == 'female'){
                    $gender=1;
                }
                $newData[]=[
                    'transaction ID'=>$v['id'],
                    'Loan ID'=>substr($v['user_loan_id'],0,30),
                    'Customer ID'=>substr($v['user_id'],0,30),
                    'First Name'=>$userCerData['firstname'],
                    'Middle Name'=>$userCerData['middlename'],
                    'Last Name'=>$userCerData['lastname'],
                    'DOB'=>$birthDay,
                    'Mobile'=>$v['user_phone'],
                    'Email'=>$userCerData['email'],
                    'Gender'=>$gender,
                    'Aadhaar Number'=>$userCerData['aadhaar_num'],
                    'PAN Number'=>$personalData['panID'],
                    'Current Address 1'=>$personalData['currentAddress'],
                    'Current Address 2'=>$personalData['currentAddress'],
                    'Current Address 3'=>$personalData['currentAddress'],
                    'Current State'=>$personalData['currentState'],
                    'Current City'=>$personalData['currentCity'],
                    'Current Pincode'=>$personalData['currentPinCode'],
                    'Permanent Address 1'=>$personalData['permanentaAddress'],
                    'Permanent Address 2'=>$personalData['permanentaAddress'],
                    'Permanent Address 3'=>$personalData['permanentaAddress'],
                    'Permanent State'=>$personalData['permanentState'],
                    'Permanent City'=>$personalData['permanentCity'],
                    'Permanent Pincode'=>$personalData['permanentaPinCode'],
                    'Bank Name'=>$bankData['card_type'],
                    'Bank Account Number'=>$bankData['ptm_card'],
                    'Bank IFSC Code'=>$bankData['nbfc_num'],
                    'Bank Branch Name'=>'',
                    'Disbursement Date'=>$disbursementDate,
                    'First repayment Date'=>$repaymentDate,
                    'Loan Amount'=>$v['money'],
                    'Tenure'=>$userLoanData['loanRepayPeriod'],
                    'ROI'=>$userLoanData['loanInterestRate'],
                    'Fee'=>$userLoanData['auditFee']
                ];
            }
            $column_name=[
                [
                    'Transaction ID',
                    'Loan ID',
                    'Customer ID',
                    'First Name',
                    'Middle Name',
                    'Last Name',
                    'DOB',
                    'Mobile',
                    'Email',
                    'Gender',
                    'Aadhaar Number',
                    'PAN Number',
                    'Current Address 1',
                    'Current Address 2',
                    'Current Address 3',
                    'Current State',
                    'Current City',
                    'Current Pincode',
                    'Permanent Address 1',
                    'Permanent Address 2',
                    'Permanent Address 3',
                    'Permanent State',
                    'Permanent City',
                    'Permanent Pincode',
                    'Bank Name',
                    'Bank Account Number',
                    'Bank IFSC Code',
                    'Bank Branch Name',
                    'Disbursement Date',
                    'First repayment Date',
                    'Loan Amount',
                    'Tenure',
                    'ROI',
                    'Fee'
                ]
            ];
            $data = array_merge($column_name,$newData);
            $this->extendToCsv($data);
//            $data = array_merge($column_name,$newData);
//            $this->extendToExcel($data);
        }

        $p=empty(input('p'))?1:input('p');
        $startIndex=($p-1)*15;
        $query['limitation']=15;
        $query['offset']=$startIndex;
        $value = $this->client->getArrayData($countUrl,$query,3);
        if($value['code'] != 0){
            $value['data']=[];
        }
        $pager=new Page(count($totalCount['data']),15,$p);
        $page=$pager->show();
        foreach($value['data'] as $k=>$v){
            $value['data'][$k]['create_time']=$this->gmtToIndiaTime($v['create_time']);
            $value['data'][$k]['update_time']=$this->gmtToIndiaTime($v['update_time']);
        }
        $this->assign('page',$page);
        $this->assign('data',$value['data']);
        $this->assign('totalCount',count($totalCount['data']));
        $this->assign('totalData',json_encode($totalCount['data']));
        return view();
    }

    /**
     * @return \think\response\View
     * 交易失败列表
     */
    public function transactionNoList(){
        $query=[];
        if(session('role') == 'admin'){
            //线上
            $query['user_phone']=[
                '$or'=>[
                    ['$like'=>'9%'],
                    ['$like'=>'8%'],
                    ['$like'=>'7%'],
                    ['$like'=>'6%'],
                    ['$like'=>'5%'],
                    ['$like'=>'4%'],
                    ['$like'=>'3%'],
                    ['$like'=>'2%']
                ],
                '$notlike'=>"1111111%"
            ];
        }elseif(session('role') == 'testing'){
            //测试
            $query['user_phone']=[
                '$like'=>'1%'
            ];
        }
        if(!empty($_GET['userId'])){
            $userId=$_GET['userId'];
            $userId=explode(' ',$userId);
            $query['user_id']=[
                '$in'=>$userId
            ];
        }
        if(isset($_GET['userPhone']) && !empty($_GET['userPhone'])){
            $query['user_phone']=$_GET['userPhone'];
        }
        if(!empty($_GET['upCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000
            ];
        }
        if(!empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        if(!empty($_GET['upCreateTime']) && !empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000,
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        $query['payed_status']='reject';
        $query['limitation']=10000;
        $countUrl=Uri::HOST.Uri::POSTSELECTUSERLOANTRANSACTION;
        $totalCount =$this->client->getArrayData($countUrl,$query,3);
        if(isset($_GET['extend']) && $_GET['extend'] == 1){
            $extendData= $totalCount['data'];
            $newData=[];
            foreach($extendData as $k=>$v){
                //查找个人认证
                $usercerUrl=Uri::HOST.Uri::USERCERITYS."?phoneNumber=".$v['user_phone'];
                $userCerData=$this->client->getArrayData($usercerUrl);
                $userCerData=$userCerData['data'][0];
                //查找绑卡信息
                $bankUrl=Uri::HOST.Uri::SELECTUSERBANK.$v["user_id"];
                $bankData=$this->client->getArrayData($bankUrl);
                $bankData=$bankData['data'];

                //查询用户personal信息
                $personalUrl=Uri::HOST.Uri::SELECTPERSONINFO."?user_id=".$v['user_id'];
                $personalData=$this->client->getArrayData($personalUrl);
                $personalData=$personalData['data'][0];


                //查询userloads信息
                $userloanUrl=Uri::HOST.Uri::SELECTUSERLOAN."?id=".$v['user_loan_id'];
                $userLoanData=$this->client->getArrayData($userloanUrl);
                $userLoanData=$userLoanData['data'][0];
                $repaymentDate=$v['update_time'];
                $repaymentDate=strtotime($repaymentDate) + 3600*24*$userLoanData['loanRepayPeriod'];
                $repaymentDate=date("d-m-Y",$repaymentDate);
                $disbursementDate=strtotime($v['create_time']);
                $disbursementDate=date("d-m-Y",$disbursementDate);

                $birthDay=$personalData['birthDay'];
                if(!empty($personalData['birthDay'])){
                    $birthDay=explode('-',$birthDay);
                    $birthDay=$birthDay[2]."-".$birthDay[1]."-".$birthDay[0];
                }else{
                    $birthDay= $birthDay;
                }

                if($personalData['genderinfo'] == 'male'){
                    $gender=0;
                }elseif($personalData['genderinfo'] == 'female'){
                    $gender=1;
                }
                $newData[]=[
                    'transaction ID'=>$v['id'],
                    'Loan ID'=>substr($v['user_loan_id'],0,30),
                    'Customer ID'=>substr($v['user_id'],0,30),
                    'First Name'=>$userCerData['firstname'],
                    'Middle Name'=>$userCerData['middlename'],
                    'Last Name'=>$userCerData['lastname'],
                    'DOB'=>$birthDay,
                    'Mobile'=>$v['user_phone'],
                    'Email'=>$userCerData['email'],
                    'Gender'=>$gender,
                    'Aadhaar Number'=>$userCerData['aadhaar_num'],
                    'PAN Number'=>$personalData['panID'],
                    'Current Address 1'=>$personalData['currentAddress'],
                    'Current Address 2'=>$personalData['currentAddress'],
                    'Current Address 3'=>$personalData['currentAddress'],
                    'Current State'=>$personalData['currentState'],
                    'Current City'=>$personalData['currentCity'],
                    'Current Pincode'=>$personalData['currentPinCode'],
                    'Permanent Address 1'=>$personalData['permanentaAddress'],
                    'Permanent Address 2'=>$personalData['permanentaAddress'],
                    'Permanent Address 3'=>$personalData['permanentaAddress'],
                    'Permanent State'=>$personalData['permanentState'],
                    'Permanent City'=>$personalData['permanentCity'],
                    'Permanent Pincode'=>$personalData['permanentaPinCode'],
                    'Bank Name'=>$bankData['card_type'],
                    'Bank Account Number'=>$bankData['ptm_card'],
                    'Bank IFSC Code'=>$bankData['nbfc_num'],
                    'Bank Branch Name'=>'',
                    'Disbursement Date'=>$disbursementDate,
                    'First repayment Date'=>$repaymentDate,
                    'Loan Amount'=>$v['money'],
                    'Tenure'=>$userLoanData['loanRepayPeriod'],
                    'ROI'=>$userLoanData['loanInterestRate'],
                    'Fee'=>$userLoanData['auditFee']
                ];
            }
            $column_name=[
                [
                    'Transaction ID',
                    'Loan ID',
                    'Customer ID',
                    'First Name',
                    'Middle Name',
                    'Last Name',
                    'DOB',
                    'Mobile',
                    'Email',
                    'Gender',
                    'Aadhaar Number',
                    'PAN Number',
                    'Current Address 1',
                    'Current Address 2',
                    'Current Address 3',
                    'Current State',
                    'Current City',
                    'Current Pincode',
                    'Permanent Address 1',
                    'Permanent Address 2',
                    'Permanent Address 3',
                    'Permanent State',
                    'Permanent City',
                    'Permanent Pincode',
                    'Bank Name',
                    'Bank Account Number',
                    'Bank IFSC Code',
                    'Bank Branch Name',
                    'Disbursement Date',
                    'First repayment Date',
                    'Loan Amount',
                    'Tenure',
                    'ROI',
                    'Fee'
                ]
            ];
            $data = array_merge($column_name,$newData);
            $this->extendToCsv($data);
//            $data = array_merge($column_name,$newData);
//            $this->extendToExcel($data);
        }

        $p=empty(input('p'))?1:input('p');
        $startIndex=($p-1)*15;
        $query['limitation']=15;
        $query['offset']=$startIndex;
        $value = $this->client->getArrayData($countUrl,$query,3);
        if($value['code'] != 0){
            $value['data']=[];
        }
        $pager=new Page(count($totalCount['data']),15,$p);
        $page=$pager->show();
        foreach($value['data'] as $k=>$v){
            $value['data'][$k]['create_time']=$this->gmtToIndiaTime($v['create_time']);
            $value['data'][$k]['update_time']=$this->gmtToIndiaTime($v['update_time']);
        }
        $this->assign('page',$page);
        $this->assign('data',$value['data']);
        $this->assign('totalCount',count($totalCount['data']));
        $this->assign('totalData',json_encode($totalCount['data']));
        return view();
    }

    /**
     * 交易成功
     */
    public function  transactionSuccess(){
        $transactionId=$_POST['uid'];
        $transactionUrl=Uri::HOST.Uri::SELECTUSERLOANTRANSACTION."?id=".$transactionId;
        $transactionData=$this->client->getArrayData($transactionUrl);
        $transactionData=$transactionData['data'][0];
        $user_loan_id=$transactionData['user_loan_id'];
        $user_id=$transactionData['user_id'];
        $userUrl=Uri::HOST.Uri::USERLIST.'?id='.$user_id ;
        //查找ID
        $userData=$this->client->getArrayData($userUrl);
        $deviceId=$userData['data'][0]['deviceId'];
        $url=Uri::HOST.Uri::TRANSACYIONSUCCESS ;
        $data[]=[
           'user_loan_id'=>$user_loan_id,
           'deviceId'=>$deviceId,
           'operator_name'=>session('username'),
           'operator_id'=>session('userid')
        ];
//        dd($data);die;
        $res=$this->client->getArrayData($url,$data,3);
        return $res;
    }

    /**
     * 交易失败
     */
    public function transactionFailed(){
        $transactionId = $_POST['uuid'];
        $transactionUrl=Uri::HOST.Uri::SELECTUSERLOANTRANSACTION."?id=".$transactionId;
        $transactionData=$this->client->getArrayData($transactionUrl);
        $transactionData=$transactionData['data'][0];
        $user_loan_id=$transactionData['user_loan_id'];
        $user_id=$transactionData['user_id'];
        $userUrl=Uri::HOST.Uri::USERLIST.'?id='.$user_id ;
        //查找ID
        $userData=$this->client->getArrayData($userUrl);
        $deviceId=$userData['data'][0]['deviceId'];
        $url=Uri::HOST.Uri::TRANSACYIONFAILED ;
        $data[]=[
            'user_loan_id'=>$user_loan_id,
            'deviceId'=>$deviceId,
            'operator_name'=>session('username'),
            'operator_id'=>session('userid')
        ];
        $res=$this->client->getArrayData($url,$data,3);
        return $res;
    }

    /**
     *  批量成功
     */
    public function manySuccess(){
        if(!isset($_POST['data'])){
            return (["code"=>3,"msg"=>"0x300","no user be selected!"]);
        }
       $data=$_POST['data'];
        $postData=[];
       foreach($data as $k=>$v){
           //查找deliveId
           //查找ID
           $userUrl=Uri::HOST.Uri::USERLIST.'?id='.$v['user_id'] ;
           $userData=$this->client->getArrayData($userUrl);
           if($userData['code'] == 0){
               $deviceId=$userData['data'][0]['deviceId'];
               $postData[]=[
                   'user_loan_id'=>$v['user_loan_id'],
                   'deviceId'=>$deviceId,
                   'operator_name'=>session('username'),
                   'operator_id'=>session('userid')
               ];
           }
       }
        $url=Uri::HOST.Uri::TRANSACYIONSUCCESS ;
        $res=$this->client->getArrayData($url,$postData,3);
        return $res;
    }
    /**
     * 批量不成功
     */
    public function manyUnsuccess(){
        $data=$_POST['data'];
        $postData=[];
        foreach($data as $k=>$v){
            //查找deliveId
            //查找ID
            $userUrl=Uri::HOST.Uri::USERLIST.'?id='.$v['user_id'] ;
            $userData=$this->client->getArrayData($userUrl);
            $deviceId=$userData['data'][0]['deviceId'];
            $postData[]=[
                'user_loan_id'=>$v['user_loan_id'],
                'deviceId'=>$deviceId,
                'operator_name'=>session('username'),
                'operator_id'=>session('userid')
            ];
        }
        $url=Uri::HOST.Uri::TRANSACYIONFAILED ;
        $res=$this->client->getArrayData($url,$postData,3);
        return $res;
     }

    /**
     * 交易记录
     */
    public function record(){
        $id=$_POST['uid'];
        $failedResult=$_POST['failedReson'];
        $userPhone=$_POST['userPhone'];
        $userId=$_POST['userId'];
        $url=Uri::HOST.Uri::CREATETRANSACTIONRECORD;
        $data=[
           "user_id"=>$userId,
           "loantransaction"=>$id,
           "operator_id"=>session('userid'),
           "operator_name"=>session('username'),
           "result"=>$failedResult,
           "content"=>"用户银行卡账号无效",
           "plantform"=>1
        ];
        $res=$this->client->getArrayData($url,$data,3);
        //发送短信
        $smsRes=(new Sms())->transactionFailed($userPhone,\app\home\common\Smstmp::TRANSACTIONFAILED);
        //更改loantransaction
        $updateLoanTranData=[
            "payed_info"=>1
        ];
        $updateLoanTranUrl=Uri::HOST.Uri::UPDATELOANTRANSACTION.$id;
        $updateTransactionRes=$this->client->getArrayData($updateLoanTranUrl,$updateLoanTranData,4);

        return $updateTransactionRes;
    }

    /**
     *  打款退回待交易列表
     */
    public function payFailedList(){
        $tmpUrl=Uri::HOST.Uri::SELECTSMSTMP.'?is_valid=1&plantForm=1';
        $smsTmp=$this->client->getArrayData($tmpUrl);
        if($smsTmp['code'] != 0){
            $smsTmp=[];
        }else{
            $smsTmp=$smsTmp['data'];
        }
        $this->assign('smsTmp',$smsTmp);
        $appSmsTmp=(new Smstmp())->getAppSmsTmp(2);
        $this->assign('appSmsTmp',$appSmsTmp);
        //语音推送模板
        $smsVoiceTmp=(new Smstmp())->getAppSmsTmp(3);
        $this->assign('smsVoiceTmp',$smsVoiceTmp);

        $query=[];
        $query['payed_info']=1;
        if(session('role') == 'admin'){
            //线上
            $query['user_phone']=[
                '$notlike'=>"1%"
            ];
        }elseif(session('role') == 'testing'){
            //测试
            $query['user_phone']=[
                '$like'=>'1%'
            ];
        }
        if(isset($_GET['Phone']) && !empty($_GET['Phone'])){
            $query['user_phone']=$_GET['Phone'];
        }
        if(isset($_GET['manyUserPhone']) && !empty($_GET['manyUserPhone'])){
            $manyUserPhone=trim($_GET['manyUserPhone']);
            if(!empty($manyUserPhone)){
                if(strpos($manyUserPhone,',')){
                    $newManyUserPhone=explode(',',$manyUserPhone);
                }else{
                    $manyUserPhone=explode("\r\n",$manyUserPhone);
                    $newManyUserPhone=[];
                    if(!empty($manyUserPhone)){
                        foreach($manyUserPhone as $k=>$v){
                            $newManyUserPhone[]=trim($v);
                        }
                    }
                }
                $creteNewUserPhone=implode(',',$newManyUserPhone);
                $query['user_phone']=$newManyUserPhone;
                $_GET['manyUserPhone']=$creteNewUserPhone;
            }
        }
        if(!empty($_GET['upCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000
            ];
        }
        if(!empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        if(!empty($_GET['upCreateTime']) && !empty($_GET['downCreateTime'])){
            $query['update_time']=[
                '$gte'=>strtotime($_GET['upCreateTime'])*1000,
                '$lte'=>strtotime($_GET['downCreateTime'])*1000
            ];
        }
        $query['payed_status']='pending';
        $query['limitation']=10000;
        $countUrl=Uri::HOST.Uri::POSTSELECTUSERLOANTRANSACTION;
        $totalCount =$this->client->getArrayData($countUrl,$query,3);
        if(isset($_GET['extend']) && $_GET['extend'] == 1){
            $extendData= $totalCount['data'];
            $newData=[];
            foreach($extendData as $k=>$v){
                //查找个人认证
                $usercerUrl=Uri::HOST.Uri::USERCERITYS."?phoneNumber=".$v['user_phone'];
                $userCerData=$this->client->getArrayData($usercerUrl);
                $userCerData=$userCerData['data'][0];
                //查找绑卡信息
                $bankUrl=Uri::HOST.Uri::SELECTUSERBANK.$v["user_id"];
                $bankData=$this->client->getArrayData($bankUrl);
                $bankData=$bankData['data'];

                //查询用户personal信息
                $personalUrl=Uri::HOST.Uri::SELECTPERSONINFO."?user_id=".$v['user_id'];
                $personalData=$this->client->getArrayData($personalUrl);
                $personalData=$personalData['data'][0];

                //查询userloads信息
                $userloanUrl=Uri::HOST.Uri::SELECTUSERLOAN."?id=".$v['user_loan_id'];
                $userLoanData=$this->client->getArrayData($userloanUrl);
                $userLoanData=$userLoanData['data'][0];
                $repaymentDate=$v['update_time'];
                $repaymentDate=strtotime($repaymentDate) + 3600*24*($userLoanData['loanRepayPeriod']-1);
                $repaymentDate=date("d-m-Y",$repaymentDate);
                $disbursementDate=strtotime($v['create_time']);
                $disbursementDate=date("d-m-Y",$disbursementDate);

                $birthDay=$personalData['birthDay'];
                if(!empty($personalData['birthDay'])){
                    $birthDay=explode('-',$birthDay);
                    $birthDay=$birthDay[2]."-".$birthDay[1]."-".$birthDay[0];
                }else{
                    $birthDay= $birthDay;
                }

                if($personalData['genderinfo'] == 'male'){
                    $gender=0;
                }elseif($personalData['genderinfo'] == 'female'){
                    $gender=1;
                }
                $newData[]=[
                    'transaction ID'=>$v['id'],
                    'Loan ID'=>substr($v['user_loan_id'],0,30),
                    'Customer ID'=>substr($v['user_id'],0,30),
                    'First Name'=>$userCerData['firstname'],
                    'Middle Name'=>$userCerData['middlename'],
                    'Last Name'=>$userCerData['lastname'],
                    'DOB'=>$birthDay,
                    'Mobile'=>$v['user_phone'],
                    'Email'=>$userCerData['email'],
                    'Gender'=>$gender,
                    'Aadhaar Number'=>$userCerData['aadhaar_num'],
                    'PAN Number'=>$personalData['panID'],
                    'Current Address 1'=>$personalData['currentAddress'],
                    'Current Address 2'=>$personalData['currentAddress'],
                    'Current Address 3'=>$personalData['currentAddress'],
                    'Current State'=>$personalData['currentState'],
                    'Current City'=>$personalData['currentCity'],
                    'Current Pincode'=>$personalData['currentPinCode'],
                    'Permanent Address 1'=>$personalData['permanentaAddress'],
                    'Permanent Address 2'=>$personalData['permanentaAddress'],
                    'Permanent Address 3'=>$personalData['permanentaAddress'],
                    'Permanent State'=>$personalData['permanentState'],
                    'Permanent City'=>$personalData['permanentCity'],
                    'Permanent Pincode'=>$personalData['permanentaPinCode'],
                    'Bank Name'=>$bankData['card_type'],
                    'Bank Account Number'=>$bankData['ptm_card'],
                    'Bank IFSC Code'=>$bankData['nbfc_num'],
                    'Bank Branch Name'=>'',
                    'Disbursement Date'=>$disbursementDate,
                    'Loan Amount'=>$v['money'],
                    'Tenure'=>$userLoanData['loanRepayPeriod'],
                    'ROI'=>$userLoanData['loanInterestRate'],
                    'Fee'=>$userLoanData['auditFee'],
                    'First repayment Date'=>$repaymentDate,
                ];
            }
            $column_name=[
                [
                    'Transaction ID',
                    'Loan ID',
                    'Customer ID',
                    'First Name',
                    'Middle Name',
                    'Last Name',
                    'DOB',
                    'Mobile',
                    'Email',
                    'Gender',
                    'Aadhaar Number',
                    'PAN Number',
                    'Current Address 1',
                    'Current Address 2',
                    'Current Address 3',
                    'Current State',
                    'Current City',
                    'Current Pincode',
                    'Permanent Address 1',
                    'Permanent Address 2',
                    'Permanent Address 3',
                    'Permanent State',
                    'Permanent City',
                    'Permanent Pincode',
                    'Bank Name',
                    'Bank Account Number',
                    'Bank IFSC Code',
                    'Bank Branch Name',
                    'Disbursement Date',
                    'Loan Amount',
                    'Tenure',
                    'ROI',
                    'Fee',
                    'First repayment Date'
                ]
            ];
            $data = array_merge($column_name,$newData);
            $this->extendToCsv($data);
//            $data = array_merge($column_name,$newData);
//            $this->extendToExcel($data);
        }

        $p=empty(input('p'))?1:input('p');
        $startIndex=($p-1)*15;
        $query['limitation']=15;
        $query['offset']=$startIndex;
        $value = $this->client->getArrayData($countUrl,$query,3);
        if($value['code'] != 0){
            $value['data']=[];
        }
        $pager=new Page(count($totalCount['data']),15,$p);
        $page=$pager->show();
        $this->assign('page',$page);
        foreach($value['data'] as $k=>$v){
            $value['data'][$k]['create_time']=$this->gmtToIndiaTime($v['create_time']);
            $value['data'][$k]['update_time']=$this->gmtToIndiaTime($v['update_time']);
        }
        $this->assign('data',$value['data']);
        $phone=[];
        foreach ($value['data'] as $k=>$v) {
          $phone[]=[
              'userPhone'=>$v['user_phone'],
              'userID'=>$v['user_id']
          ];
        }
        $this->assign('phone',json_encode($phone));
        $this->assign('totalCount',count($totalCount['data']));
        $this->assign('allData',$totalCount['data']);
        $this->assign('totalData',json_encode($totalCount['data']));
        return view();
    }

    /**
     * 提交借款到KB
     */
    public function sendLoanToKb(){
      $res=Db::table("userloantransaction")
          ->where("payed_status","pending")
          ->where("payed_id",null)
          ->update(["payed_id"=>1]);
        return [
            "code"=>0,
            "msg"=>"0x200",
            "data"=>"success"
        ];
    }
}